package com.wonders;

import javax.servlet.AsyncContext;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

import static java.lang.Thread.sleep;

/**
 * 模拟耗时任务
 */
public class Task2 implements Runnable {
    private AsyncContext asyncContext;

    Task2(AsyncContext asyncContext) {
        this.asyncContext = asyncContext;
    }

    @Override
    public void run() {
        try {
//            sleep(10000);
            HttpServletRequest request = (HttpServletRequest) this.asyncContext.getRequest();
            HttpSession session = request.getSession();
            int sum = 0;
            for (int i = 0; i <= 10; i++) {
                session.setAttribute("sum", sum);
                sum += i;
                System.out.println("i == " + i);
                sleep(1000);
            }
            session.setAttribute("sum", sum);
        } catch (InterruptedException e) {
            System.out.println(e);
        }
    }

}
